package top.maof.book.rank.book;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.maof.book.model.BookInfo;
import top.maof.book.rank.Handle;
import top.maof.book.rank.HandleChain;
import top.maof.book.service.BookInfoService;
import top.maof.book.service.ShelfService;
import top.maof.book.type.HotType;

@Component
public class CollectionBookHandleRank implements Handle<BookHo> {

    @Autowired
    private BookInfoService bookInfoService;

    @Autowired
    private ShelfService shelfService;

    @Override
    public void add(BookHo bookHo, HandleChain handleChain) {
        if (bookHo.getHotType() == HotType.COLLECTION) {
            int num = shelfService.shelfNum(bookHo.getId());
            BookInfo bookInfo = new BookInfo();
            bookInfo.setCollectionNum(num);
            bookInfoService.saveOrUpdate(bookInfo);
        }

        handleChain.add(bookHo, handleChain);
    }

    @Override
    public void sub(BookHo bookHo, HandleChain handleChain) {
        if (bookHo.getHotType() == HotType.COLLECTION) {
            int num = shelfService.shelfNum(bookHo.getId());
            BookInfo bookInfo = new BookInfo();
            bookInfo.setCollectionNum(num);
            bookInfoService.saveOrUpdate(bookInfo);
        }
        handleChain.sub(bookHo, handleChain);
    }
}
